package org.infodavid.common.ejb;

import javax.ejb.Local;
import javax.ejb.PrePassivate;
import javax.ejb.Remote;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Version;

import org.hibernate.annotations.ForeignKey;
import org.hibernate.annotations.Index;
import org.hibernate.annotations.OptimisticLockType;
import org.hibernate.annotations.Persister;
import org.hibernate.annotations.Type;
import org.joda.time.DateTime;

import org.infodavid.common.dto.IUserListItem;
import org.infodavid.common.impl.model.UserConnectionImpl;
import org.infodavid.common.model.IUser;
import org.infodavid.common.model.IUserConnection;
import org.infodavid.common.model.IUserSession;
import org.infodavid.common.model.impl.persisters.VersionSafeSingleTableEntityPersister;
import org.infodavid.common.persistence.UserConnectionDataService;

/**
 * The Class UserConnectionBean.
 */
@Entity(name = "IUserConnection")
@org.hibernate.annotations.Entity(dynamicUpdate = true, selectBeforeUpdate = true, dynamicInsert = true, optimisticLock = OptimisticLockType.VERSION)
@Persister(impl = VersionSafeSingleTableEntityPersister.class)
@Table(name = "USER_CONNECTIONS")
@NamedQueries(value = {
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND_COUNT, query = "select count(d."
        + IUserConnection.KEY + ") from IUserConnection d"),
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND, query = "select d from IUserConnection d"),
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND_ACTIVE_COUNT, query = "select count(d."
        + IUserConnection.KEY + ") from IUserConnection d where d.archived = 0"),
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND_ACTIVE, query = "select d from IUserConnection d where d.archived = 0"),
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND_BY_CREATION_DATE, query = "select d from IUserConnection d where d.archived = 0 and d."
        + IUserConnection.CREATION_DATE + " between :date1 and :date2"),
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND_BY_CREATION_DATE_COUNT, query = "select count(d."
        + IUserConnection.KEY
        + ") from IUserConnection d where d.archived = 0 and d."
        + IUserConnection.CREATION_DATE + " between :date1 and :date2"),
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND_BY_MODIFICATION_DATE, query = "select d from IUserConnection d where d.archived = 0 and d."
        + IUserConnection.MODIFICATION_DATE + " between :date1 and :date2"),
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND_BY_MODIFICATION_DATE_COUNT, query = "select count(d."
        + IUserConnection.KEY
        + ") from IUserConnection d where d.archived = 0 and d."
        + IUserConnection.MODIFICATION_DATE + " between :date1 and :date2"),
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND_CONNECTED_BY_USER, query = "select distinct d from IUserConnection d, IUserSession s where d.archived = 0 and d."
        + IUserConnection.APPROVAL_STATUS
        + " = 'APPROVED' and s."
        + IUserSession.SIGNED_OUT
        + " = 0 and s."
        + IUserSession.LAST_ACCESS_DATE
        + " > :date1 and (s."
        + IUserSession.USER
        + '.'
        + IUser.KEY
        + " = d."
        + IUserConnection.RECIPIENT
        + '.'
        + IUser.KEY
        + " and d."
        + IUserConnection.INITIATOR
        + '.'
        + IUser.KEY
        + " = :"
        + IUser.ID
        + ") or (s."
        + IUserSession.USER
        + '.'
        + IUser.KEY
        + " = d."
        + IUserConnection.INITIATOR
        + '.'
        + IUser.KEY
        + " and d."
        + IUserConnection.RECIPIENT
        + '.'
        + IUser.KEY
        + " = :"
        + IUser.ID
        + ')'),
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND_CONNECTED_BY_USER_COUNT, query = "select count(distinct d."
        + IUserConnection.KEY
        + ") from IUserConnection d, IUserSession s where d.archived = 0 and d."
        + IUserConnection.APPROVAL_STATUS
        + " = 'APPROVED' and s."
        + IUserSession.SIGNED_OUT
        + " = 0 and s."
        + IUserSession.LAST_ACCESS_DATE
        + " > :date1 and (s."
        + IUserSession.USER
        + '.'
        + IUser.KEY
        + " = d."
        + IUserConnection.RECIPIENT
        + '.'
        + IUser.KEY
        + " and d."
        + IUserConnection.INITIATOR
        + '.'
        + IUser.KEY
        + " = :"
        + IUser.ID
        + ") or (s."
        + IUserSession.USER
        + '.'
        + IUser.KEY
        + " = d."
        + IUserConnection.INITIATOR
        + '.'
        + IUser.KEY
        + " and d."
        + IUserConnection.RECIPIENT
        + '.'
        + IUser.KEY
        + " = :"
        + IUser.ID
        + ')'),
    @NamedQuery(name = UserConnectionDataService.QUERY_FIND_BY_USERS, query = "select d from IUserConnection d where d.archived = 0 and d."
        + IUserConnection.RECIPIENT
        + '.'
        + IUser.KEY
        + " = :"
        + IUserConnection.RECIPIENT
        + " and d." + IUserConnection.INITIATOR + '.' + IUser.KEY + " = :" + IUser.ID)
})
@Local(value = IUserConnection.class)
@Remote(value = IUserConnectionRemote.class)
public class UserConnectionBean extends UserConnectionImpl {

	/** The Constant serialVersionUID. */
	private static final long serialVersionUID = 1660345027625851664L;

	/**
	 * The Constructor.
	 * @throws Exception the exception
	 */
	protected UserConnectionBean() throws Exception {
		super();
	}

	/**
	 * The Constructor.
	 * @param keyArg the key arg
	 * @throws Exception the exception
	 */
	protected UserConnectionBean(final Long keyArg) throws Exception {
		super(keyArg);
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.auth.model.impl.UserConnectionImpl#getComment()
	 */
	@Override
	@Column(name = COMMENT, length = 1024, nullable = true)
	public String getComment() {
		return super.getComment();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.UserConnectionImpl#getConnection()
	 */
	@Override
	@JoinColumn(name = RECIPIENT, referencedColumnName = IUser.ID, nullable = false, updatable = false)
	@ManyToOne(targetEntity = UserBean.class, fetch = FetchType.LAZY, optional = false)
	@ForeignKey(name = "FK_USER_CONNECTIONS_CONNECTION")
	public IUserListItem getRecipient() {
		return super.getRecipient();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#getCreationDate()
	 */
	@Override
	@Column(name = CREATION_DATE, nullable = false, updatable = false)
	@Type(type = "org.infodavid.persistence.impl.hibernate.usertype.DateTimeUserType")
	@Index(name = "IDX_USER_CONNECTIONS_CREATIONDATE")
	public DateTime getCreationDate() {
		return super.getCreationDate();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#getKey()
	 */
	@Override
	@Id
	@GeneratedValue(strategy = GenerationType.IDENTITY)
	@Column(name = ID, nullable = false)
	public Long getKey() {
		return super.getKey();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#getModificationDate()
	 */
	@Override
	@Version
	@Column(name = MODIFICATION_DATE, nullable = false)
	@Type(type = "org.infodavid.persistence.impl.hibernate.usertype.DateTimeUserType")
	public DateTime getModificationDate() {
		return super.getModificationDate();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.UserConnectionImpl#getApprovalStatus()
	 */
	@Override
	@Enumerated(EnumType.STRING)
	@Column(name = APPROVAL_STATUS, nullable = true, length = 32, updatable = true)
	public EApprovalStatus getApprovalStatus() {
		return super.getApprovalStatus();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.UserConnectionImpl#getUser()
	 */
	@Override
	@JoinColumn(name = IUser.ID, referencedColumnName = IUser.ID, nullable = false, updatable = false)
	@ManyToOne(targetEntity = UserBean.class, fetch = FetchType.LAZY, optional = false)
	@ForeignKey(name = "FK_USER_CONNECTIONS_USER")
	public IUserListItem getInitiator() {
		return super.getInitiator();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.UserConnectionImpl#init()
	 */
	@Override
	@PrePassivate
	public void init() throws Exception {
		super.init();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.impl.model.DataObjectImpl#isArchived()
	 */
	@Override
	@Column(name = ARCHIVED, nullable = false)
	@Index(name = "IDX_USER_CONNECTIONS_ARCHIVED")
	public boolean isArchived() {
		return super.isArchived();
	}
}
